package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/4/17
 * @description
 */
@RestController
@RequestMapping("course")
@Api(tags = "课程模块")
public class CourseController {


    @Resource
    private CourseService courseService;

    @GetMapping("list")
    @ApiOperation("课程列表")
    public ResponseResult courseList() {

        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.select(Course::getCourseName, Course::getId);

        List<Course> list = courseService.list(lambda);
        return ResponseResult.success().data("list", list);

    }

    @PostMapping("page")
    @ApiOperation("课程分页")
    public ResponseResult coursePage(@RequestBody QueryCondition<Course> queryCondition) {

        return courseService.coursePage(queryCondition);

    }


    @PostMapping("addOrUpdate")
    @ApiOperation("添加或修改课程")
    public ResponseResult addOrUpdateCourse(@RequestBody Course course) {

        courseService.saveOrUpdate(course);

        return ResponseResult.success();

    }


    @GetMapping("getById/{id}")
    @ApiOperation("根据id获取课程信息")
    public ResponseResult getCourseById(@PathVariable("id") Integer id) {

        Course course = courseService.getById(id);

        return ResponseResult.success().data("course", course);

    }


    @DeleteMapping("delete/{id}")
    @ApiOperation("根据id获取课程信息")
    public ResponseResult deleteCourseById(@PathVariable("id") Integer id) {


        courseService.removeById(id);

        return ResponseResult.success().message("删除成功");

    }

}
